package com.atguigu.srb.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * 微服务跨域配置
 * @author lupengning
 * @version 1.0
 * @description:
 * @date 2021/6/11 2:58
 */
@Configuration
public class CorsConfig {
  @Bean
  public CorsWebFilter corsFilter() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true); //是否允许携带cookie
    config.addAllowedOrigin("*"); //可接受的域，是一个具体域名或者*（代表任意域名）
    config.addAllowedHeader("*"); //允许携带的头
    config.addAllowedMethod("*"); //允许访问的方式

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);

    return new CorsWebFilter(source);
  }
}
